﻿<?
	include "config.php";
	include("seguranca.php"); // Inclui o arquivo com o sistema de segurança
    protegePagina(); // Chama a função que protege a página
	
	if($_SESSION['nivel'] != 'administrador'){ //Se não for ADM desloga o usuário e expulsa da página
		expulsaVisitante();
	}
?>
<html>
<head>
    <title>Gerenciar Professores</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" href="css/bootstrap.css" />
</head>

<body>
    <div class="container">
      <header class="masthead">
      <h1 class="muted" align="center">Gerenciar Professores</h1>
		<?
			# DELETE
			if(isset($_GET['action']) && $_GET['action'] == 'delete'){
				$id = (int)$_GET['id'];

				$sqlDelete = 'DELETE FROM professores WHERE id = :id';

				try {
					$delete = $db->prepare($sqlDelete);
					$delete->bindValue(':id', $id, PDO::PARAM_INT);
					if($delete->execute()){
						echo "<div class='alert alert-success'>
						<button type='button' class='close' data-dismiss='alert'>&times;</button>
						<strong>Deletado com sucesso!</strong>
						</div>";
					}
				} catch (PDOException $e) {
					echo "<div class='alert alert-error'>
						<button type='button' class='close' data-dismiss='alert'>&times;</button>
						<strong>Erro ao deletar dados!</strong>" . $e->getMessage() . "
						</div>";
				}
			}
			?>
            
            
            <article>
			
			<section class="jumbotron">
                        
            <table class="table table-hover">
					
					<thead>
						<tr>
							<th>ID</th>
							<th>Nome:</th>
                            <th>Usuário:</th>
							<th>E-mail:</th>
							<th>Escola:</th>
						</tr>
					</thead>
        <tbody>
    	<?
		    
        $rs = $db->query("SELECT * FROM `professores` ORDER BY nome");
        while ($linha = $rs->fetch(PDO::FETCH_ASSOC)) { ?>
        <tr>
            <td><?php echo "{$linha['id']}" ?></td>
            <td><?php echo "{$linha['nome']}" ?></td>
			<td><?php echo "{$linha['usuario']}" ?></td>
			<td><?php echo "{$linha['email']}" ?></td>
			<td><?php echo "{$linha['escola']}" ?></td>
            <td>
                <a href="atualizarProfessor.php?action=atualizar&id=<?php 
                    echo "{$linha['id']}"; ?>" class="btn"><i class="icon-pencil"></i></a>
                <a href="gerenciarProfessor.php?action=delete&id=<?php echo $linha['id']; ?>" class="btn" 
                    onclick="return confirm('Deseja deletar?');"><i class="icon-remove"></i></a>
            </td>
        </tr>
   	<? } ?>
	 </tbody>
	</table>
<script src="js/jQuery.js"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>
